package com.hlju.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlju.reggie.bean.Setmeal;
import com.hlju.reggie.bean.SetmealDish;
import com.hlju.reggie.bean.dto.SetmealDto;
import com.hlju.reggie.common.CustException;
import com.hlju.reggie.mapper.SetmealMapper;
import com.hlju.reggie.service.SetmealDishService;
import com.hlju.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    @Transactional
    public void saveSetmealWithDish(SetmealDto dto) {
        //保存数据到套餐表
        this.save(dto);
        //获取传递过来的菜品集合(用关系表的实体类接受)
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //设置套餐id
            setmealDish.setSetmealId(dto.getId());
        }
        //批量保存关系
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealDto getSetmealWithDish(Long id) {
        //获取套餐信息
        Setmeal setmeal = this.getById(id);
        //创建dto对象
        SetmealDto dto = new SetmealDto();
        //属性赋值
        BeanUtils.copyProperties(setmeal,dto);
        //根据套餐id查询套餐菜品关系表
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",id);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);
        //给dto设置关联的菜品集合
        dto.setSetmealDishes(setmealDishes);
        //返回dto
        return dto;
    }

    @Override
    @Transactional
    public void updateSetmealWithDish(SetmealDto dto) {
        //修改套餐信息
        this.updateById(dto);
        //根据套餐id删除关系表的数据
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id",dto.getId() );
        setmealDishService.remove(wrapper);

        //将传递过来的菜品关系数据加入到关系表
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    @Transactional
    public void deleteSetmealWithDish(Long[] ids) throws CustException {
        //根据传入过来的id 查询出是否有正在售卖（启用）的套餐
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        wrapper.in("id",ids);

        //根据条件查询符合的数量
        int count = this.count(wrapper);
        if(count > 0){
            System.out.println("符合条件的套餐数量===>"+count);
            //抛出一个自定义异常
            throw new CustException("不能删除正在售卖的商品");
        }
        //批量删除套餐
        this.removeByIds(Arrays.asList(ids));
        //根据套餐id删除关系表中对应数据
        QueryWrapper<SetmealDish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.in("setmeal_id",ids);
        //批量删除
        setmealDishService.remove(dishQueryWrapper);
    }
}
